{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#包含helloworld与颜色空间的作业"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##第一个opencv程序，显示并修改窗口名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "img = cv.imread(r'temp.jpg')\n",
    "cv.imshow('hello world',img)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "opencv的第一步，希望不会掉队"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[111 112 113 ...  23  23  23]\n",
      " [111 112 114 ...  23  23  24]\n",
      " [108 109 114 ...  23  23  24]\n",
      " ...\n",
      " [ 85  94 109 ...  23  34  34]\n",
      " [ 50  60  90 ... 168   0  15]\n",
      " [ 40  43  53 ... 171 174   0]]\n",
      "[[ 42  36  41 ...  90  93 104]\n",
      " [ 42  35  37 ...  88  90 107]\n",
      " [ 44  36  35 ... 105  97 104]\n",
      " ...\n",
      " [ 35  31  36 ...  34  26  57]\n",
      " [ 26  19  25 ...  32  29  36]\n",
      " [ 20  14  23 ...  56  50  43]]\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "image = cv.imread(r'temp.jpg')\n",
    "img = cv.pyrDown(image)\n",
    "gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)\n",
    "\n",
    "cv.imshow('source',img)\n",
    "cv.imshow('gray',gray)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)\n",
    "cv.imshow('HSV',hsv)\n",
    "\n",
    "zeros = np.zeros(img.shape[:2],dtype=\"uint8\")#创建与image相同大小的零矩阵用于填充通道数为0\n",
    "\n",
    "#分离三通道 显示HSV\n",
    "print(hsv[:,:,0])#输出H通道的参数矩阵\n",
    "cv.imshow('Hue',hsv[:,:,0])\n",
    "cv.imshow('Saturation',hsv[:,:,1])\n",
    "cv.imshow('value',hsv[:,:,2])\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "\n",
    "#分离三通道 显示BGR，同时将其他通道置于0\n",
    "B,G,R=cv.split(img)\n",
    "print(B)#输出B通道矩阵，知识数值与上述的H通道不同\n",
    "B=cv.merge([B,zeros,zeros])\n",
    "G=cv.merge([zeros,G,zeros])\n",
    "R=cv.merge([zeros,zeros,R])\n",
    "cv.imshow('B',B)\n",
    "cv.imshow('G',G)\n",
    "cv.imshow('R',R)\n",
    "cv.waitKey()\n",
    "\n",
    "cv.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "学习了颜色空间的转化，以及图像通道的分离，第一次运行的时候发现分离出来的单通道与老师显示的不一致，检查之后发现如果想要让图片显示BGR单通道同时还要显示结果，需要将其他两个通道数设为0，但是HSV显示却无法将其他两个通道数设为0再显示，使用merge合并之后再imshow显示的仍然是BGR的形式，不知道哪里出现了问题。\n",
    "所以HSV没法分析，尝试666了但是没有查找到结果，猜想\n",
    "①imshow在使用的时候默认显示的BGR格式，但是hsv转换颜色空间之后imshow可以显示出来。\n",
    "②merge合成的时候默认形成了BGR，因为三通道参数说白了就是矩阵格式0~255的数值，merge的定义中没有参数，所以它合并是怎么识别我的三个参数是BGR的还是HSV格式的？\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
